<template>
  <div class="app-container" v-loading="pageLoading">
    <el-button type="primary" class="fr" @click="createItem" v-permission="'moManage.add'">创建</el-button>
    <el-button type="success" class="fr" @click="importVisible = true" style="margin-right: 5px"
      v-permission="'moManage.add'">导入</el-button>
    <div class="filter-container">
      <el-input style="width: 300px" placeholder="工单号/品号/备注" clearable prefix-icon="el-icon-search" class="filter-item"
        @keyup.enter.native="research" @clear="research" v-model="query.query_string"></el-input>
      <el-select v-model="query.status" class="filter-item" placeholder="请选择状态">
        <el-option label="未上线" value="0"></el-option>
        <el-option label="已上线" value="1"></el-option>
        <el-option label="生产中" value="2"></el-option>
        <el-option label="已完成" value="5"></el-option>
        <el-option label="--- 正常结单" value="3"></el-option>
        <el-option label="--- 手工结单" value="4"></el-option>
        <el-option label="所有工单" value="6"></el-option>
      </el-select>
      <el-button class="filter-item" type="success" plain @click="research">查询</el-button>
      <el-button class="filter-item" type="info" plain @click="exportData" :loading="exportLoading">导出 </el-button>
    </div>
    <div style="margin-bottom: 5px">
      <el-button type="info" size="mini" v-show="currentMo.id" @click="showMoInfo" plain :disabled="!currentMo">工单信息
      </el-button>
      <el-button type="primary" v-permission="'moManage.edit'"
        v-show="currentMo.mo_status == '0' || currentMo.mo_status == '1'" size="mini" @click="moOline(0)" plain>
        工单上线
      </el-button>
      <el-button type="primary" v-permission="'moManage.edit'" v-show="currentMo.mo_status == '2'" size="mini"
        @click="moOline(1)" plain>
        机台模具绑定</el-button>
      <el-button type="success" v-permission="'moManage.edit'" v-show="currentMo.mo_status == '1'" size="mini"
        @click="openMoSnDialog" plain>
        展开流水号</el-button>
      <el-button type="warning" v-permission="'moManage.edit'"
        v-show="currentMo.mo_status == '0' || currentMo.mo_status == '1' || currentMo.mo_status == '2'" size="mini"
        @click="changeStatus('4')" plain>手工结单
      </el-button>
      <el-button type="warning" v-permission="'moManage.edit'" @click="changeStatus('2')"
        v-show="currentMo.mo_status == '4'" size="mini" plain>
        取消手工结单</el-button>
    </div>
    <z-table :list="list" :tableProps="tableProps" :columns="columns" @row-click="moClick">
      <template v-slot:content="v">
        <template v-if="v.key === 'mo_no'">
          <a href="#" @click.prevent="goMoDetail(v.row)" class="text-blue text-bold"> {{ v.row['mo_no'] }}</a>
        </template>
        <template v-else-if="v.key === 'route_code_name'">
          <a @click.prevent="goCnDetail(v.row)" class="text-blue text-bold"> {{ v.row['route_code_name'] }}</a>
        </template>
        <template v-else-if="v.key === 'sn_qty'">
          <a @click.prevent="goSnDetail(v.row)" class="text-blue text-bold"> {{ v.row['sn_qty'] }}</a>
        </template>
        <span v-else-if="v.key === 'mo_status_name'" class="text-label" :class="moStatusClass[v.row['mo_status']]">
          {{ v.row['mo_status_name'] }}
        </span>
        <span v-else>{{ v.row[v.key] }}</span>
      </template>
    </z-table>
    <z-pagination :pagination="pagination" :total="total" :page.sync="query.page" :limit.sync="query.size"
      @change="getList"></z-pagination>

    <!-- 工单上线 与绑定机台 的modal -->
    <el-dialog :title="name" :data="data" width="80%" :close-on-click-modal="false" :visible.sync="editFormVisible">
      <div style="text-align: center; margin-bottom: 20px; margin-top: -20px">
        <span v-if="step == 0" class="text-bold text-green">步骤1：完善工单信息</span>
        <span v-else-if="step == 1" class="text-bold text-green">步骤2：绑定机台模具</span>
      </div>
      <div v-if="step == 0" style="padding-right: 100px">
        <z-form :data="data" :formProps="formProps" :fields="fields"></z-form>
        <div class="text-right">
          <el-button @click="editFormVisible = false">取消</el-button>
          <el-button type="primary" @click.native="submit" :loading="submitLoading"> 保存并继续下一步 </el-button>
        </div>
      </div>
      <div v-else-if="step == 1" style="padding: 0 100px">
        <el-form :inline="true" :model="machineInfo">
          <el-form-item label="机台">
            <el-select v-model="machineInfo.machine_id" filterable style="width: 280px">
              <el-option v-for="m in machineOptions" :key="m.id" :value="m.id"
                :label="m.machine_code + ' ' + m.machine_name"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="模具">
            <el-select v-model="machineInfo.tooling_id" filterable style="width: 280px">
              <el-option v-for="t in toolingOptions" :key="t.id" :value="t.id"
                :label="t.tooling_id + ' ' + t.tooling_name">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="addMachineInfo">添加</el-button>
          </el-form-item>
        </el-form>
        <z-table :list="boundList" :tableProps="tableProps2" :columns="columns2">
          <template v-slot:operation="v">
            <a href="#" class="text-red" @click.prevent="deleteMachineInfo(v.row, v.$index)">删除</a>
          </template>
        </z-table>
        <div class="text-right" style="margin-top: 20px">
          <el-button @click="editFormVisible = false">取消</el-button>
          <el-button type="primary" @click.native="moOnlineSubmit" :loading="submitLoading">
            {{ currentMo.mo_status == '2' ? '保存' : '工单上线' }}
          </el-button>
        </div>
      </div>
    </el-dialog>

    <!-- 展开流水号 modal -->
    <el-dialog title="展开流水号" :visible.sync="moSnDialog" :close-on-click-modal="false" width="30%">
      <el-form ref="form" :model="moSnData">
        <el-form-item label="单个流水号的产品数量">
          <el-input-number v-model="moSnData.BATCH_QTY" :step="1" :min="1" :precision="0">
          </el-input-number>
        </el-form-item>
      </el-form>
      <div class="text-right">
        <el-button @click="moSnDialog = false">取消</el-button>
        <el-button type="primary" @click.native="createSn" :loading="submitLoading">确认</el-button>
      </div>
    </el-dialog>

    <!-- 工单信息查看的modal -->
    <el-dialog title="工单信息" :visible.sync="moInfoDialog" :close-on-click-modal="false" width="80%">
      <div style="padding-right: 100px">
        <z-form :data="currentMo" :formProps="formProps3" :fields="fields3"></z-form>
        <el-form label-width="140px">
          <el-form-item label="已绑定机台">
            <z-table :list="boundList" :tableProps="tableProps3" :columns="columns2"></z-table>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>

    <el-dialog title="导入" width="50%" :visible.sync="importVisible">
      <div style="padding-right: 60px">
        <div style="margin-bottom: 10px"><a href="#" @click.prevent="getTemp">下载导入模板</a></div>
        <upload-excel-component ref="uploadExcel" :on-success="handleSuccess" :before-upload="beforeUpload"
          style="margin-bottom: 20px" />
        <el-button type="primary" @click.native="importSave" :loading="importSaveLoading"> 确认 </el-button>
        <el-button @click="importVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import commonMixin from '@/views/_common/mixin'
import dayjs from 'dayjs'
import UploadExcelComponent from '@/components/UploadExcel/index.vue'
export default {
  mixins: [commonMixin],
  components: { UploadExcelComponent },
  name: 'moManage',
  data: function () {
    return {
      api: this.$api.mo,
      name: '工单管理',
      columns: [
        { title: '工单编号', key: 'mo_no', width: 130, fixed: 'left' },
        // { title: '父阶工单', key: 'parent_mo_no', width: 130 },
        { title: '工单状态', key: 'mo_status_name', width: 85, fixed: 'left' },
        { title: '工单类型', key: 'mo_type', width: 120 },
        { title: '目标数量', key: 'target_qty', width: 80 },
        { title: '序号数量', key: 'sn_qty', width: 80 },
        { title: '创建日期', key: 'mo_create_time', width: 100 },
        { title: '上线日期', key: 'mo_plan_time', width: 100 },
        { title: '预计产出日期', key: 'mo_target_time', width: 100 },
        { title: '产品料号', key: 'key_part_no', width: 120 },
        { title: '品名', key: 'key_part_name', width: 140 },
        { title: '简码', key: 'brief_code', width: 120 },
        { title: '工艺流程编号/名称(版本)', key: 'route_code_name', width: 200 },
        { title: 'BOM(版本)', key: 'bom_no_text', width: 150 },
        { title: '投入线别', key: 'line_name', width: 100 },
        { title: '客户', key: 'cust_code', width: 120 },
        { title: '客户料号', key: 'cust_pn_code', width: 120 },
        { title: '投入量', key: 'input_qty', width: 80 },
        { title: '产出量', key: 'output_qty', width: 80 },
        { title: '备注', key: 'remarks', width: 140 },
      ],
      fields: [
        {
          title: '工单编号',
          key: 'mo_no',
          span: 12,
          required: true,
          events: {
            input: (value) => {
              value = value.replace(/^[A-Z]/, '')
              console.log(value, this.api)
              return value
            },
          },
        },
        { title: '工单类型', key: 'mo_type', span: 12, name: 'select', props: { filterable: true }, required: true },
        { title: '投入线别', key: 'default_line', span: 12, name: 'select', options: [], required: true },
        // { title: '父阶工单', key: 'parent_mo_no', span: 12 },
        {
          title: 'BOM',
          key: 'bom_id',
          span: 12,
          name: 'select',
          options: [],
          props: {
            filterable: true,
            remote: true,
            remoteMethod: null,
          },
          required: true,
        },
        { title: '目标数量', key: 'target_qty', name: 'number', span: 12, required: true, props: { min: 1 } },
        {
          title: '产品料号',
          key: 'material_id',
          span: 12,
          name: 'select',
          options: [],
          props: {
            filterable: true,
            remote: true,
            remoteMethod: null,
          },
          events: { change: null },
          required: true,
        },
        {
          title: '客户',
          key: 'cust_id',
          required: true,
          span: 12,
          name: 'select',
          props: { filterable: true },
          events: { change: null },
        },
        { title: '客户料号', key: 'cust_pn_id', span: 12, name: 'select', props: { filterable: true }, options: [] },
        {
          title: '工艺流程',
          key: 'route_id',
          span: 24,
          name: 'select',
          options: [],
          props: { filterable: true },
          required: true,
        },
        {
          title: '上线日期',
          key: 'mo_plan_time',
          span: 12,
          name: 'date',
          props: { 'value-format': 'yyyy-MM-dd' },
          required: true,
        },
        {
          title: '预计产出日期',
          key: 'mo_target_time',
          span: 12,
          name: 'date',
          props: { 'value-format': 'yyyy-MM-dd' },
          required: true,
        },
        { title: '备注', key: 'remarks', span: 24, name: 'textarea', required: false },
      ],
      query: {
        status: '6',
        query_string: null,
        size: 10,
        page: 1,
      },
      tableProps: { opsColWith: '190px', border: true, highlightCurrentRow: true, hideOperations: true },
      apiList: this.$api.mo + 'getbymostatus',
      apiEdit: this.$api.mo + 'online',
      moSnDialog: false,
      moSnData: { BATCH_QTY: 1 },
      routeCloumList: {},
      moStatus: { 0: '未上线', 1: '已上线', 2: '生产中', 3: '正常结单', 4: '手工结单' },
      moStatusClass: { 0: 'bg-red', 1: 'bg-green', 2: 'bg-yellow', 3: 'bg-blue', 4: 'bg-blue' },
      statusData: {},
      importVisible: false,
      importSaveLoading: false,
      initData: {
        mo_create_time: dayjs().format('YYYY-MM-DD'),
        cust_pn_id: '',
        material_id: '',
        cust_id: '',
        route_id: '',
        mo_status: '0',
        input_qty: 0,
        output_qty: 0,
      },
      currentMo: {},
      bomList: [],
      step: 0,
      machineInfo: {},
      machineOptions: [],
      toolingOptions: [],
      boundList: [],
      moInfoDialog: false,
      tableProps2: { border: true },
      columns2: [
        { title: '机台编号', key: 'machine_code' },
        { title: '机台名称', key: 'machine_name' },
        { title: '模具编号', key: 'tooling_code' },
        { title: '模具名称', key: 'tooling_name' },
      ],
      tableProps3: { border: true, hideOperations: true },
      formProps3: {
        labelWidth: '140px',
        disabled: true,
      },
      fields3: [
        { title: '工单编号', key: 'mo_no', span: 8 },
        { title: '工单类型', key: 'mo_type', span: 8 },
        { title: '投入线别', key: 'line_name', span: 8 },
        { title: '产品料号', key: 'key_part_no', span: 8 },
        { title: '客户代码', key: 'cust_code', span: 8 },
        { title: '客户料号', key: 'cust_pn_code', span: 8 },
        { title: 'BOM编码(版本)', key: 'bom_no_text', span: 16 },
        { title: '工单状态', key: 'mo_status_name', span: 8 },
        { title: '工艺流程(版本)', key: 'route_code_name', span: 24 },
        { title: '计划时间', key: 'mo_plan_time', span: 8 },
        { title: '目标产出时间', key: 'mo_target_time', span: 8 },
        { title: '创建时间', key: 'mo_create_time', span: 8 },
        { title: '计划数量', key: 'target_qty', span: 8 },
        { title: '已投入数量', key: 'input_qty', span: 8 },
        { title: '已产出数量', key: 'output_qty', span: 8 },
        { title: '备注', key: 'remarks', span: 24, name: 'textarea' },
      ],
    }
  },
  methods: {
    // 工单号点击
    goMoDetail(row) {
      this.$store.dispatch('delView', { name: 'moManageInfo' }).then((r) => {
        this.$router
          .push({
            name: 'moManageInfo',
            params: {
              id: row.mo_no,
              bom_id: row.bom_id,
              route_id: row.route_id,
            },
          })
          .catch(() => {})
      })
    },
    // 工艺流程点击
    goCnDetail(row) {
      this.$store.dispatch('delView', { name: 'processDetail' }).then((r) => {
        this.$router
          .push({
            name: 'processDetail',
            params: {
              id: row.route_id,
            },
          })
          .catch(() => {})
      })
    },
    // sn数量点击
    goSnDetail(row) {
      this.$store.dispatch('delView', { name: 'moSerialManage' }).then((r) => {
        this.$router
          .push({
            name: 'moSerialManage',
            params: {
              mo: row.mo_no,
            },
          })
          .catch(() => {})
      })
    },
    moClick(v) {
      this.currentMo = { ...v }
    },
    clearMo() {
      this.currentMo = {}
    },
    showMoInfo() {
      this.moInfoDialog = true
      this.getBoundInfo()
    },
    createItem() {
      this.setFieldOptions('cust_pn_id', [])
      this.setFieldOptions('route_id', [])
      this.setFieldOptions('bom_id', [])
      this.data = _.cloneDeep(this.initData)
      this.step = 0
      this.editFormVisible = true
    },
    formatBeforeSave(data) {
      let tmp = _.cloneDeep(data)
      const reg = /^[0-9A-Z]+$/
      if (reg.test(tmp.mo_no)) {
        return tmp
      } else {
        this.$message.error('工单号只能为数字或大写字母')
        return false
      }
    },
    submit() {
      let url = this.apiEdit
      let data = this.formatBeforeSave(this.data)
      if (!data) return
      this.submitLoading = true
      this.$request(url, data, 'POST')
        .then((r) => {
          this.submitLoading = false
          this.$message({
            message: '操作成功',
            type: 'success',
          })
          //进入下一步
          this.step++
          this.currentMo = data
          this.moOline(1)
          //拉取更新列表
          this.getList()
        })
        .catch(() => {
          this.submitLoading = false
        })
    },
    moOnlineSubmit() {
      if (this.boundList.length == 0) {
        this.$confirm('你没有选择要绑定的机台，确定要继续吗', '操作确认').then(() => {
          this.moOnlineSubmitConfirm()
        })
      } else {
        this.moOnlineSubmitConfirm()
      }
    },
    moOnlineSubmitConfirm() {
      this.submitLoading = true
      this.$request(
        this.$api.mo + 'bindMachineAndTooling',
        {
          mo_id: this.currentMo.id,
          mo_no: this.currentMo.mo_no,
          list: this.boundList,
        },
        'POST',
      )
        .then((r) => {
          this.submitLoading = false
          this.$message({
            message: '操作成功',
            type: 'success',
          })
          this.editFormVisible = false
          this.clearMo()
          //拉取更新列表
          this.getList()
        })
        .catch(() => {
          this.submitLoading = false
        })
    },
    changeStatus(v) {
      if (!this.currentMo.mo_no) {
        this.$message({
          message: '没有选择工单',
          type: 'error',
        })
        return
      }
      this.$prompt('请输入' + (v == '4' ? '结单' : '取消结单') + '操作的原因').then(({ value }) => {
        this.$request(
          this.api + 'status/update',
          {
            remarks: value,
            status: v,
            mo_id: this.currentMo.id,
          },
          'POST',
        ).then((r) => {
          this.$message({
            message: '操作成功',
            type: 'success',
          })
          this.getList()
          this.clearMo()
        })
      })
    },
    formatList(list) {
      var list = _.cloneDeep(list)
      list.forEach((v) => {
        v.mo_status_name = this.moStatus[v['mo_status']]
        v.route_code_name = v.route_code + ' ' + v.route_name + ' (' + v.version + ')'
        v.bom_no_text = v.bom_no + ' (' + v.bom_version + ')'
        v.mo_create_time = v.mo_create_time ? v.mo_create_time.substring(0, 10) : ''
        v.mo_plan_time = v.mo_plan_time ? v.mo_plan_time.substring(0, 10) : ''
        v.mo_target_time = v.mo_target_time ? v.mo_target_time.substring(0, 10) : ''
      })
      return list
    },
    //选项菜单相关
    getSkuOptions(str) {
      this.$request(this.$api.sku + 'getlist', { queryString: str, page: 1, size: 99 }).then((r) => {
        this.setFieldOptions('material_id', r.list, 'id', ['key_part_no', 'key_part_name'])
      })
    },
    getBomOptions(str) {
      this.$request(this.$api.bom + 'getlistByGroup', { queryString: str, page: 1, size: 99 }).then((r) => {
        this.bomList = r.list
        this.setFieldOptions('bom_id', r.list, 'id', ['bom_no', 'bom_version'], ['编号：', '版本：'])
      })
    },
    skuChange() {
      this.getCustSkuBySku()
      this.getRouteBySku()
    },
    custChange() {
      this.getCustSkuBySku()
    },
    getRouteBySku() {
      this.setFieldOptions('route_id', [])
      if (!this.data.material_id) return
      this.$request(this.$api.design + 'getRouteBySku', { sku: this.data.material_id }).then((r) => {
        this.setFieldOptions('route_id', r, 'id', ['route_name', 'version'], ['', ' 版本:'])
      })
    },
    getCustSkuBySku() {
      this.setFieldOptions('cust_pn_id', [])
      if (!this.data.material_id || !this.data.cust_id) return
      this.$request(this.$api.partmap + 'getCusSkuByMskuAndCustCode', {
        sku: this.data.material_id,
        cust: this.data.cust_id,
      }).then((r) => {
        this.setFieldOptions('cust_pn_id', r, 'id', ['cust_pn_code', 'cust_pn_name'])
      })
    },
    //展开序号相关
    openMoSnDialog() {
      this.moSnData = { BATCH_QTY: 1 }
      this.$request(this.$api.param + 'getbyname', {
        name: 'EXPAND_SN',
        type: 'BATCH',
      }).then((r) => {
        if (r.length) {
          if (r[0].param_value.toLowerCase() == 'true') {
            this.moSnDialog = true
            if (r[0].param_condition1 != '' && r[0].param_condition1 != null) {
              this.moSnData.BATCH_QTY = r[0].param_condition1
            }
          }
          if (r[0].param_value.toLowerCase() == 'false') {
            this.$confirm('确定要展开序号吗？', '操作确认').then(() => {
              this.createSn()
            })
          }
        } else {
          this.$message({
            message: '没有获取到展开序号的系统参数，请确认。',
            type: 'error',
          })
        }
      })
    },
    createSn() {
      this.submitLoading = true
      this.$request(
        this.$api.mo + 'createsn',
        {
          mo_id: this.currentMo.id,
          mo_no: this.currentMo.mo_no,
          batch_qty: this.moSnData.BATCH_QTY,
        },
        'post',
      )
        .then((r) => {
          this.submitLoading = false
          this.$message({
            message: '操作成功',
            type: 'success',
          })
          this.moSnDialog = false
          this.clearMo()
          this.getList()
        })
        .catch(() => {
          this.submitLoading = false
        })
    },
    //上线相关
    moOline(type) {
      if (type == 0) {
        this.step = 0
        this.pageLoading = true
        this.getSkuOptions(this.currentMo.sku_no)
        this.$request(this.$api.mo + 'getbyid', {
          id: this.currentMo.id,
        })
          .then((r) => {
            this.pageLoading = false
            this.data = r
            this.editFormVisible = true
            this.getCustSkuBySku()
            this.getRouteBySku()
            this.$request(this.$api.bom + 'getlistByGroup', {
              queryString: this.data.bom_no,
              page: 1,
              size: 99,
            }).then((r) => {
              this.setFieldOptions('bom_id', r.list, 'id', ['bom_no', 'bom_version'], ['编号：', '版本：'])
            })
          })
          .catch(() => {
            this.pageLoading = false
          })
      } else if (type == 1) {
        this.step = 1
        this.boundList = []
        this.machineInfo = {}
        //获取机台选项
        this.$request(this.$api.machine + 'getCanUseListByMo', {
          mo_no: this.currentMo.mo_no,
        }).then((r) => {
          this.machineOptions = r
        })
        //获取模具选项
        this.$request(this.$api.tooling + 'getCanUseListByMo', {
          mo_no: this.currentMo.mo_no,
        }).then((r) => {
          this.toolingOptions = r
        })
        //获取已绑定的列表
        this.getBoundInfo()
        this.editFormVisible = true
      }
    },
    getBoundInfo() {
      this.pageLoading = true
      this.$request(this.$api.mo + 'getMoBoundInfo', {
        mo_no: this.currentMo.mo_no,
      })
        .then((r) => {
          this.pageLoading = false
          this.boundList = r
        })
        .catch(() => {
          this.pageLoading = false
        })
    },
    addMachineInfo() {
      let ma = this.machineOptions.find((i) => i.id == this.machineInfo.machine_id)
      let to = this.toolingOptions.find((i) => i.id == this.machineInfo.tooling_id)
      console.log(to, this.machineInfo.tooling_id, this.toolingOptions)
      if (!ma) {
        this.$message({
          message: '机台必须选择',
          type: 'error',
        })
        return
      }
      if (this.boundList.find((i) => i.machine_id == this.machineInfo.machine_id)) {
        this.$message({
          message: '此机台已经绑定到这个工单，请选择其他机台',
          type: 'error',
        })
        return
      }
      // if (this.boundList.find((i) => i.tooling_id == this.machineInfo.tooling_id)) {
      //   this.$message({
      //     message: '此模具已经绑定到这个工单，请选择其他模具',
      //     type: 'error',
      //   })
      //   return
      // }
      this.boundList.push({
        machine_id: ma.id,
        machine_code: ma.machine_code,
        machine_name: ma.machine_name,
        tooling_id: to ? to.id : null,
        tooling_code: to ? to.tooling_id : null,
        tooling_name: to ? to.tooling_name : null,
      })
      this.machineInfo = {}
    },
    deleteMachineInfo(row, i) {
      this.boundList.splice(i, 1)
    },
    // 上传下载相关
    beforeUpload(file) {
      const isLt1M = file.size / 1024 / 1024 < 50
      if (isLt1M) {
        return true
      }
      this.$message({
        message: 'Please do not upload files larger than 1m in size.',
        type: 'warning',
      })
      return false
    },
    handleSuccess({ results, header }) {
      this.excelData = [[]]
      results.forEach((v) => {
        let tmp = []
        header.forEach((i) => {
          tmp.push(v[i])
        })
        this.excelData.push(tmp)
      })
      /*this.tableHeader = header*/
    },
    importSave: function () {
      let url = this.api + 'batchadd'
      if (this.excelData.length <= 0) {
        this.$message({
          message: '请先选择Excel',
          type: 'error',
        })
        return false
      }
      this.$request(url, this.excelData, 'post')
        .then((r) => {
          this.importSaveLoading = false
          this.$message({
            message: '操作成功',
            type: 'success',
          })
          this.importVisible = false
          this.excelData = [[]]
          this.$refs.uploadExcel.clearFile()
          this.getList()
        })
        .catch(() => {
          this.importSaveLoading = false
        })
    },
    getTemp() {
      import('@/vendor/Export2Excel')
        .then((excel) => {
          excel.export_json_to_excel({
            header: [
              '工单号（必填）',
              '机种(model)',
              '工单类型(必填)',
              '数量（必填）',
              '上线日期（必填,格式:20210401）',
              '预计产出日期（必填,格式:20210401）',
              'SKU（必填）',
              '客户料号(cust_sku)',
              '客户(cust_code)',
              'BOM编号（必填）',
              'BOM版本（必填）',
              '备注',
            ],
            data: [[]],
            filename: '工单导入模板',
          })
        })
        .catch(() => {})
    },
  },
  created: function () {
    this.getList()
    this.$request(this.$api.line + 'getlistbypage', {}).then((r) => {
      this.setFieldOptions('default_line', r.list, 'id', 'line_name')
    })
    this.$request(this.$api.dictionary + 'getchildren', { parentType: 'mo_type' }).then((r) => {
      this.setFieldOptions('mo_type', r, 'value', 'value')
    })
    this.$request(this.$api.custom + 'getList', { page: 1, size: 99 }).then((r) => {
      this.setFieldOptions('cust_id', r.list, 'id', ['cust_code', 'cust_name'])
    })
    this.getFieldConfig('material_id').props.remoteMethod = this.getSkuOptions
    this.getFieldConfig('bom_id').props.remoteMethod = this.getBomOptions
    this.getFieldConfig('material_id').events.change = this.skuChange
    this.getFieldConfig('cust_id').events.change = this.custChange
  },
}
</script>
<style scoped>
</style>